/* De communicatie van de Applet naar de server
 * kan via GET (en POST) dingen (bijv. pagina.jsp?a=1)
 * (via post: javascript <input type="hidden">)
 * De communicatie van de server naar de Applet
 * kan via <param>
 * Er wordt geen gebruik gemaakt van frameset
 * maar de applet staat gewoon op elke pagina.
 */

import java.applet.AppletContext;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.URL;
import java.util.Calendar;
import java.util.GregorianCalendar;
import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.Timer;
//import java.lang.RuntimePermission;

public class Applet extends JApplet {

  AppletContext appletContext;
  boolean isIngevoerd = false;

  public boolean getisIngevoerd() {
    return isIngevoerd;
  }
  String hour = "";
  String minute = "";
  String second = "";
  String dag = "";
  String dagNr = "";
  String maand = "";
  String jaar = "";

  public void setCurrentTime() {
    Calendar calendar = new GregorianCalendar();
    hour = Integer.toString(calendar.get(Calendar.HOUR_OF_DAY));
    minute = Integer.toString(calendar.get(Calendar.MINUTE));
    second = Integer.toString(calendar.get(Calendar.SECOND));
    if (calendar.get(Calendar.HOUR_OF_DAY) < 10) {
      hour = "0" + calendar.get(Calendar.HOUR_OF_DAY);
    }
    if (calendar.get(Calendar.MINUTE) < 10) {
      minute = "0" + calendar.get(Calendar.MINUTE);
    }
    if (calendar.get(Calendar.SECOND) < 10) {
      second = "0" + calendar.get(Calendar.SECOND);
    }
    int t = 0;
    t = calendar.get(Calendar.DAY_OF_WEEK);
    switch (t) {
      case 1: {
        dag = "Zo";
        break;
      }
      case 2: {
        dag = "Ma";
        break;
      }
      case 3: {
        dag = "Di";
        break;
      }
      case 4: {
        dag = "Wo";
        break;
      }
      case 5: {
        dag = "Do";
        break;
      }
      case 6: {
        dag = "Vri";
        break;
      }
      case 7: {
        dag = "Za";
        break;
      }
      default: {
        dag = "" + t;
        break;
      }
    }
    dagNr = Integer.toString(calendar.get(Calendar.DAY_OF_MONTH));
    t = calendar.get(Calendar.MONTH);
    switch (t) {
      case 0: {
        maand = "Januari";
        break;
      }
      case 1: {
        maand = "Februari";
        break;
      }
      case 2: {
        maand = "Maart";
        break;
      }
      case 3: {
        maand = "April";
        break;
      }
      case 4: {
        maand = "Mei";
        break;
      }
      case 5: {
        maand = "Juni";
        break;
      }
      case 6: {
        maand = "Juli";
        break;
      }
      case 7: {
        maand = "Augustus";
        break;
      }
      case 8: {
        maand = "September";
        break;
      }
      case 9: {
        maand = "Oktober";
        break;
      }
      case 10: {
        maand = "November";
        break;
      }
      case 11: {
        maand = "December";
        break;
      }
      default: {
        maand = "" + (t - 1);
        break;
      }
    }
    jaar = Integer.toString(calendar.get(Calendar.YEAR));
  }
  MagTekTrackReader mtcr;

  @Override
  public void start() {
    setBackground(new Color(82, 140, 164));
    createDingen();
    setCurrentTime();
    repaint();
    //JOptionPane.showMessageDialog(null,"FOO");
    try {
      mtcr = new MagTekTrackReader();
      mtcr.ejectCard();
      mtcr.consumeCard();
    } catch (Exception ex) {
      JOptionPane.showMessageDialog(null, ex);
    }
    Timer ti = new Timer(100, new ActionListener() {

      public void actionPerformed(ActionEvent arg0) {
        //JOptionPane.showMessageDialog(null, "Insert card plz!!!!");
        setCurrentTime();
        repaint();
        stepDingen();
      }
    });
    ti.start();
    ti.setRepeats(true);
    setCurrentTime();
  }

  public void stepDingen() {
    //if (getParameter("pagina").equals("index")) {
    //Op de index-pagina moet er worden gecheckt of
    //de pas als is ingevoerd.
    //Zo ja: door naar code.jsp
    ////////
    if (getParameter("pagina") != null && getParameter("pagina").equals("index")) {
      //Checken of er een (echte) pas is/wordt ingevoerd
JOptionPane.showMessageDialog(null, "Insert card plz");
      try {
        if (!mtcr.consumed) {
          
          //Thread.sleep(1000);
          mtcr.consumeCard();
        //Thread.sleep(1000);
        } else {

          track3 = "null";
          while (track3.equals("null")) {
            track3 = mtcr.readTrack((byte) 0x03);
          }
          System.out.println("track 3 " + track3);
          track2 = "null";
          while (track2.equals("null")) {
            track2 = mtcr.readTrack((byte) 0x02);
          }
          System.out.println("track 2 " + track2);

          track1 = "null";
          while (track1.equals("null")) {
            track1 = mtcr.readTrack((byte) 0x01);
          }
          System.out.println("track 1 " + track1);
          //mtcr.ejectCard();
          try {
            String ip = getCodeBase().getHost();
            //JOptionPane.showMessageDialog(null, "BOE"+ip+" HOI");
            //JOptionPane.showMessageDialog(null,"http://" + ip + ":8080/Prj3-4Pin/testPas.jsp");
            //JOptionPane.showMessageDialog(null,"foo");
            String argsi = "t1=" + track1 + "&t2=" + track2 + "&t3=" + track3;
            argsi = argsi.replaceAll("\\?", "!");
            argsi = argsi.replaceAll("[%]", "_");
            //URL adres = new URL("http://" + ip + ":8080/prj4Pin/pasInvoeren.jsp?"+argsi);
            URL adres = new URL(getCodeBase() + "/pasInvoeren.jsp?" + argsi);
            //JOptionPane.showMessageDialog(null, adres.toString());
            //System.out.println(adres.toString());
            appletContext = getAppletContext();
            appletContext.showDocument(adres);
          //appletContext.showDocument(adres, "_blank");
          //JOptionPane.showMessageDialog(null, ip);
          } catch (Exception ex) {
            //VERKEERDE URL
            JOptionPane.showMessageDialog(null, "FOUT !\n" + ex);
          }
        }
      } catch (Exception ex) {
        JOptionPane.showMessageDialog(null, "FOUT !\n" + ex);
      }
    ////////
    }
  }
  JButton knop;
  public static String track1 = "";
  public static String track2 = "";
  public static String track3 = "";
  public static boolean eersteFrame = true;
  public static boolean testPas = false;

  public void createDingen() {
    JPanel bovensteP = new JPanel();
    JPanel ondersteP = new JPanel();
    bovensteP.setBounds(0, 0, 350, 50);
    ondersteP.setBounds(0, 51, 350, 127);
    add(bovensteP);
    add(ondersteP);
    if (getParameter("pagina") != null && getParameter("pagina").equals("print")) {
      //JOptionPane.showMessageDialog(null,"Print");
      //
      setCurrentTime();
      String[] tekst = new String[7];
      tekst[0] = "Lucky Bank";
      tekst[2] = "Pinautomaatstraat 1";
      tekst[1] = "" + dag + " " + dagNr + " " + maand + " " + jaar + " " + hour + ":" + minute + ":" + second;
      //tekst[3] = "" + getParameter("Mutatie_id") + " | " + getParameter("automaatnummer");
      tekst[3] = "Automaatnr: " + getParameter("automaatnummer");
      //tekst[3] = "t3";
      //tekst[4] = "t4";
      //tekst[5] = "t5";
      //tekst[6] = "t6";
      tekst[4] = "" + getParameter("pasnummer") + " | " + getParameter("rekeningnummer");
      tekst[5] = "Opgenomen bedrag: " + getParameter("bedrag");
      tekst[6] = "(Huidige) Saldo: " + getParameter("saldo");
      //JOptionPane.showMessageDialog(null,tekst[2]);
      new deBon(tekst, deBon.logoImg);
      //Doorsturen naar stop.jsp
      try {
        //String ip = getCodeBase().getHost();
        //JOptionPane.showMessageDialog(null, "BOE"+ip+" HOI");
        //JOptionPane.showMessageDialog(null,"http://" + ip + ":8080/Prj3-4Pin/testPas.jsp");
        //JOptionPane.showMessageDialog(null,"foo");
        //String argsi = "t1=" + track1 + "&t2=" + track2 + "&t3=" + track3;
        //argsi = argsi.replaceAll("\\?", "!");
        //argsi = argsi.replaceAll("[%]", "_");
        //URL adres = new URL("http://" + ip + ":8080/prj4Pin/pasInvoeren.jsp?"+argsi);
        URL adres = new URL(getCodeBase() + "/stop.jsp");
        //JOptionPane.showMessageDialog(null, adres.toString());
        //System.out.println(adres.toString());
        appletContext = getAppletContext();
        appletContext.showDocument(adres);
      //appletContext.showDocument(adres, "_blank");
      //JOptionPane.showMessageDialog(null, ip);
      } catch (Exception ex) {
        //VERKEERDE URL
        JOptionPane.showMessageDialog(null, "FOUT !\n" + ex);
      }
    }
    if (getParameter("pagina") != null && getParameter("pagina").equals("index")) {
      //
      //Maak een Virtuele Testpas Knop
      knop = new JButton("Virtuele Testpas");
      knop.setBounds(5, 50, 180, 40);
      knop.setVisible(true);
      //knop.repaint();
      setLayout(null);
      ondersteP.add(knop);
      knop.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {
          //Doen alsof de pas WEL is ingevoerd
          isIngevoerd = true;
          //De Nep-tracks maken
          track1 = "%240005^DETester^1512?";
          track2 = ";240005=0005?";
          track3 = ";240005=24?";
          track1 = JOptionPane.showInputDialog("Geef Track1", track1);
          track2 = JOptionPane.showInputDialog("Geef Track2", track2);
          track3 = JOptionPane.showInputDialog("Geef Track3", track3);
          testPas = true;
          //Naar code.jsp
          //Redirect
          try {
            String ip = getCodeBase().getHost();
            //JOptionPane.showMessageDialog(null, "BOE"+ip+" HOI");
            //JOptionPane.showMessageDialog(null,"http://" + ip + ":8080/Prj3-4Pin/testPas.jsp");
            //JOptionPane.showMessageDialog(null,"foo");
            String argsi = "t1=" + track1 + "&t2=" + track2 + "&t3=" + track3;
            argsi = argsi.replaceAll("\\?", "!");
            argsi = argsi.replaceAll("[%]", "_");
            //URL adres = new URL("http://" + ip + ":8080/prj4Pin/pasInvoeren.jsp?"+argsi);
            URL adres = new URL(getCodeBase() + "/pasInvoeren.jsp?" + argsi);
            //JOptionPane.showMessageDialog(null, adres.toString());
            //System.out.println(adres.toString());
            appletContext = getAppletContext();
            appletContext.showDocument(adres);
          //appletContext.showDocument(adres, "_blank");
          //JOptionPane.showMessageDialog(null, ip);
          } catch (Exception ex) {
            //VERKEERDE URL
            JOptionPane.showMessageDialog(null, "FOUT !\n" + ex);
          }
        }
      });
    }
  }

  @Override
  public void paint(Graphics g) {
    if (eersteFrame) {
      g.clearRect(0, 0, 350, 127);
      if (getParameter("pagina") != null && getParameter("pagina").equals("index")) {
        knop.repaint();
      }
      eersteFrame = false;
    } else {
      g.clearRect(0, 0, 350, 50);
    }
    //g.setColor(Color.black);
    Font font4 = new Font("Helvetica", Font.BOLD, 16);
    g.setFont(font4);
    g.drawString(dag + " " + dagNr + " " + maand + " " + jaar + " " + hour + ":" + minute + ":" + second, 5, 20);
  }

  public void print() {
    //deBon((String[]) bericht,(String) plaatjelokatie);
    }

  public static void beginMethod(String[] args) {
    JFrame frame = new JFrame();
    Applet applet = new Applet();
    frame.add(applet);
    applet.init();
    applet.start();
    frame.setSize(350, 127);
    frame.setLocationRelativeTo(null);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
  }
}
